﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using W.Common.Enum;
using W.Warehouse.Api.Application.Command.StorageCommand;
using W.Warehouse.Domain;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.StorageCommandHandlder
{
    public class StorageFanCommandHandlder : IRequestHandler<StorageFanCommand, ApiResult<FanStorageDTO>>
    {
        private readonly ILogger<StorageFanCommandHandlder> _logger;
        private readonly IStorageRepository _storageRepository;
        private readonly IStorageProductsRepository _storageProductsRepository;
        public StorageFanCommandHandlder(ILogger<StorageFanCommandHandlder> logger, IStorageRepository storageRepository, IStorageProductsRepository storageProductsRepository)
        {
            _logger = logger;
            _storageRepository = storageRepository;
            _storageProductsRepository = storageProductsRepository;
        }
        public async Task<ApiResult<FanStorageDTO>> Handle(StorageFanCommand request, CancellationToken cancellationToken)
        {
            ApiResult<FanStorageDTO> result = new ApiResult<FanStorageDTO>();
            try
            {
                _logger.LogInformation("反填入库成功");
                var products = await _storageProductsRepository.GetAllAsync();
                var storage = from a in await _storageRepository.GetAllAsync()
                              where a.IsDelete == false && a.StorageId == request.storageId
                              select new FanStorageDTO
                              {
                                  StorageNo = a.StorageNo,
                                  StorageId = a.StorageId,
                                  AssociateNumber = a.AssociateNumber,
                                  VendorNo = a.VendorNo,
                                  VendorName = a.VendorName,
                                  VendorContact = a.VendorContact,
                                  VendorPhone = a.VendorPhone,
                                  SingleMaker = a.SingleMaker,
                                  SingleTime = a.SingleTime,
                                  StorageRemarks = a.StorageRemarks,
                                  StorageStatus = a.StorageStatus,
                                  StorageStatusTest = a.StorageStatus.ToString(),
                                  StorageType = a.StorageType,
                                  ExamineTime = a.ExamineTime,
                                  ExaminePeople = a.ExaminePeople,
                                  storageProductData = products.Where(x => x.StorageId == a.StorageId).Select(x => new StorageProductData
                                  {
                                      StorageProductsId = x.StorageProductsId,
                                      ProductNo = x.ProductNo,
                                      ProductName = x.ProductName,
                                      ProductSpec = x.ProductSpec,
                                      Batch = x.Batch,
                                      Price = x.Price,
                                      StorageNum = x.StorageNum,
                                      TotalPrice = x.TotalPrice,
                                      BinLocationName = x.BinLocationName,
                                      StorageId = x.StorageId,
                                  }).ToList()
                              };
                result.Data = storage.FirstOrDefault();
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
            }
            return result;
        }
    }
}
